package com.honeyms.project.blog.blogcontent.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.honeyms.project.blog.blogcontent.mapper.BlogContentMapper;
import com.honeyms.project.blog.blogcomment.mapper.BlogCommentMapper;
import com.honeyms.project.blog.blogcontent.domain.BlogContent;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.blog.blogcontentdetail.domain.BlogContentDetail;
import com.honeyms.project.blog.blogcontentdetail.mapper.BlogContentDetailMapper;
import com.honeyms.project.blog.blogfavoritesusercontent.mapper.BlogFavoritesUserContentMapper;
import com.honeyms.project.blog.blogzanusercontent.mapper.BlogZanUserContentMapper;
import com.honeyms.common.utils.security.ShiroUtils;
import com.honeyms.common.utils.text.Convert;
import com.honeyms.framework.aspectj.lang.annotation.DataScope;

/**
 * 帖子内容Service业务层处理
 * 
 * @author honeyms
 * @date 2020-10-12
 */
@Service
public class BlogContentServiceImpl implements IBlogContentService 
{
    @Autowired
    private BlogContentMapper blogContentMapper;
    
    @Autowired
    private BlogContentDetailMapper blogContentDetailMapper;
    
    @Autowired
    private BlogCommentMapper blogCommentMapper;
    
    @Autowired
    private BlogZanUserContentMapper blogZanUserContentMapper;
    
    @Autowired
    private BlogFavoritesUserContentMapper blogFavoritesUserContentMapper;
    
    /**
     * 查询帖子内容
     * 
     * @param contentId 帖子内容ID
     * @return 帖子内容
     */
    @Override
    public BlogContent selectBlogContentById(Long contentId)
    {
        return blogContentMapper.selectBlogContentById(contentId);
    }

    /**
     * 查询帖子内容列表
     * 
     * @param blogContent 帖子内容
     * @return 帖子内容
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BlogContent> selectBlogContentList(BlogContent blogContent)
    {
        return blogContentMapper.selectBlogContentList(blogContent);
    }
    
    /**
     * 查询帖子内容列表
     * 
     * @param blogContent 帖子内容
     * @return 随机帖子内容
     */
    @Override
    public List<BlogContent> selectBlogContentListByFollow(String ids)
    {
    	return blogContentMapper.selectBlogContentListByFollow(Convert.toStrArray(ids));
    }
    
    /**
     * 随机帖子内容列表
     * 
     * @param blogContent 帖子内容
     * @return 随机帖子内容
     */
    @Override
    public List<BlogContent> selectBlogContentListByRand(BlogContent blogContent)
    {
    	return blogContentMapper.selectBlogContentListByRand(blogContent);
    }
    
    /**
     * 查询帖子内容列表
     * 
     * @param blogContent 帖子内容
     * @return 查询帖子内容
     */
    @Override
    public List<BlogContent> selectBlogContent(BlogContent blogContent)
    {
    	return blogContentMapper.selectBlogContent(blogContent);
    }

    /**
     * 查询猜你喜欢的帖子
     * 
     * @param blogContent 帖子内容
     * @return 查询猜你喜欢的帖子
     */
    @Override
    public List<BlogContent> guessYouLike(Long userId)
    {
    	return blogContentMapper.guessYouLike(userId);
    }

    /**
     * 统计某天发帖量
     * 
     * @param days 某天
     * @return 统计某天发帖量
     */
    @Override
    public int selectCounte(Long days) 
    {
    	return blogContentMapper.selectCounte(days);
    }
    
    /**
     * 统计某天内发帖量
     * 
     * @param days 某天
     * @return 统计某天内发帖量
     */
    @Override
    public int selectCountd(Long days)
    {
    	return blogContentMapper.selectCountd(days);
    }

    /**
     * 新增帖子内容
     * 
     * @param blogContent 帖子内容
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBlogContent(BlogContent blogContent)
    {
    	blogContent.setCreatedId(ShiroUtils.getUserId());
    	blogContent.setAuthor(ShiroUtils.getLoginName());
    	blogContent.setCreateDate(new Date());
    	if (blogContent.getAllowComment() == null) {
    		blogContent.setAllowComment(2);
		}
    	if (blogContent.getType() == null) {
			blogContent.setType(2);
		}
		if (blogContent.getStatus() == null) {
			blogContent.setStatus(2);
		}
		if (blogContent.getIshot() == null) {
			blogContent.setIshot(2);
		}
		if (blogContent.getIstool() == null) {
			blogContent.setIstool(2);
		}
		if (blogContentMapper.insertBlogContent(blogContent)>0) {
			BlogContentDetail blogContentDetail = new BlogContentDetail();
			blogContentDetail.setContentId(blogContent.getContentId());
			blogContentDetail.setTitle(blogContent.getTitle());
			blogContentDetail.setCreatedId(blogContent.getCreatedId());
			blogContentDetail.setCreateDate(new Date());
			return blogContentDetailMapper.insertBlogContentDetail(blogContentDetail);
		}
        return 0;
    }

    /**
     * 修改帖子内容
     * 
     * @param blogContent 帖子内容
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBlogContent(BlogContent blogContent,BlogContentDetail blogContentDetail)
    {
    	blogContent.setEditId(ShiroUtils.getUserId());
    	blogContent.setEditDate(new Date());
    	if (blogContent.getAllowComment() == null) {
    		blogContent.setAllowComment(2);
		}
		if (blogContent.getStatus() == null) {
			blogContent.setStatus(2);
		}
		if (blogContent.getIshot() == null) {
			blogContent.setIshot(2);
		}
		if (blogContent.getIstool() == null) {
			blogContent.setIstool(2);
		}
		if (blogContentMapper.updateBlogContent(blogContent)>0) {
			if (blogContentDetail!=null) {
				blogContentDetail.setEditId(blogContent.getEditId());
				blogContentDetail.setEditDate(new Date());
				return blogContentDetailMapper.updateBlogContentDetail(blogContentDetail);
			}else {
				blogContentDetail = new BlogContentDetail();
				blogContentDetail.setContentId(blogContent.getContentId());
				blogContentDetail.setTitle(blogContent.getTitle());
				blogContentDetail.setEditId(blogContent.getEditId());
				blogContentDetail.setEditDate(new Date());
				return blogContentDetailMapper.insertBlogContentDetail(blogContentDetail);
			}
		}
        return 0;
    }

    /**
     * 删除帖子内容对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBlogContentByIds(String ids)
    {
    	if (blogContentMapper.deleteBlogContentByIds(Convert.toStrArray(ids))>0) {
    		blogCommentMapper.deleteBlogContentIds(Convert.toStrArray(ids));
    		blogZanUserContentMapper.deleteBlogZanUserContentByContentIds(Convert.toStrArray(ids));
    		blogFavoritesUserContentMapper.deleteBlogFavoritesUserContentByContentIds(Convert.toStrArray(ids));
			return blogContentDetailMapper.deleteBlogContentDetailByIds(Convert.toStrArray(ids));
		}
        return 0;
    }

    /**
     * 删除帖子内容信息
     * 
     * @param contentId 帖子内容ID
     * @return 结果
     */
    @Override
    public int deleteBlogContentById(Long contentId)
    {
    	if (blogContentMapper.deleteBlogContentById(contentId)>0) {
			return blogContentDetailMapper.deleteBlogContentDetailById(contentId);
		}
        return 0;
    }
}
